var rootuserid = 0;
var lowerid = 0, lastlowerid = 0;
var lowerid2 = 0;
var totalMoneyIn = 0, totalMoneyOut = 0, totalBuyAmount = 0, totalGiveBack = 0, totalProfit = 0;
var btSelect;
var needrequest = false, needrequest2 = false;
jQuery(document).ready(function() {
    btSelect = jQuery("#btSelect");
    lowerid = lowerid2 = rootuserid;
    $j('#tabs').tabs();
    jQuery("#tb_list").jqGrid({
        url: getJQGridUrl(1),
        datatype: "json",
        colNames: ['游戏类型', '期号', '开奖号码','投注额','结果','返点', '游戏盈亏'],
        colModel: [
        { name: 'id', width: 140, align: "center", search: false },
        { name: 'username', width: 120, searchoptions: { sopt: ["eq"]} },
    	{ name: 'usertype', width: 100, align: "center", search: false },
    	{ name: 'usertype', width: 100, align: "center", search: false },
    	{ name: 'usertype', width: 80, align: "center", search: false },
    	{ name: 'moneyin', width: 100, align: "right", search: false },
        { name: 'moneyout', width: 100, align: "right", search: false }],
        pager: "#div_page",
        rowNum: 20,
        rowList: [20, 50, 100],
        height: 460,
        viewrecords: true,
        footerrow: true,
        sortname: "id",
        sortorder: "asc",
        caption: "<a href='javascript:void(0)' onclick='showLowerList(" + rootuserid + ")'>本级</a>",
        editurl: "#",
        loadJSONDataComplete: function(a) {             //服务端数据格式 username,realname,usertype,moneyin,moneyout,buyamount,giveback,profit
            if (a && a.rows && a.rows.length > 0) {
                var pos = (a.page - 1) * jQuery("#tb_list").jqGrid()[0].p.rowNum + 1;
                var sum_moneyin = 0, sum_moneyout = 0, sum_buyamount = 0, sum_giveback = 0, sum_profit = 0;
                var moneyin = 0, moneyout = 0, buyamount = 0, giveback = 0, profit = 0;
                var index = 0;
                for (var i = 0; i < a.rows.length; i++) {
                    var newcell = new Array();
                    index = 0;
                    newcell[index++] = pos++;
                    newcell[index++] = "<a href='javascript:void(0)' title='" + a.rows[i].cell[1] + "' onclick='showLowerList(" + a.rows[i].id + ")'>" + a.rows[i].cell[0] + "</a>";
                    newcell[index++] = getUserTypeHtml(a.rows[i].cell[2]);
                    moneyin = parseFloat(a.rows[i].cell[3]);
                    sum_moneyin += moneyin;
                    moneyout = parseFloat(a.rows[i].cell[4]);
                    sum_moneyout += moneyout;
                    buyamount = parseFloat(a.rows[i].cell[5]);
                    sum_buyamount += buyamount;
                    giveback = parseFloat(a.rows[i].cell[6]);
                    sum_giveback += giveback;
                    profit = parseFloat(a.rows[i].cell[7]);
                    sum_profit += profit;
                    newcell[index++] = moneyin.toFixed(2);
                    newcell[index++] = moneyout.toFixed(2);
                    newcell[index++] = buyamount.toFixed(2);
                    newcell[index++] = giveback.toFixed(2);
                    newcell[index++] = profit.toFixed(2);
                    newcell[index++] = (rootuserid == a.rows[i].id || lowerid == a.rows[i].id || a.rows[i].cell[2] == "0") ? "<a href='javascript:void(0)' onclick='showProfitRecords(" + a.rows[i].id + ")'>查看明细</a>" : "";
                    a.rows[i].cell = newcell;
                }
                if (a.totalDatas) {
                    totalMoneyIn = parseFloat(a.totalDatas[0][0]);
                    totalMoneyOut = parseFloat(a.totalDatas[0][1]);
                    totalBuyAmount = parseFloat(a.totalDatas[0][2]);
                    totalGiveBack = parseFloat(a.totalDatas[0][3]);
                    totalProfit = parseFloat(a.totalDatas[0][4]);
                    //重新设置URL
                    if (a.lowerid && a.lowerid != lowerid)
                        lowerid = a.lowerid;
                    jQuery("#tb_list").jqGrid("setGridParam", { url: getJQGridUrl(0) });
                    //检测并恢复查询按钮
                    if (btSelect)
                        btSelect.removeAttr("disabled");
                }
                jQuery("#tb_list").jqGrid("footerData", "set", { username: "本页小计<br />总合计", moneyin: sum_moneyin.toFixed(2) + "<br />" + totalMoneyIn.toFixed(2), moneyout: sum_moneyout.toFixed(2) + "<br />" + totalMoneyOut.toFixed(2), buyamount: sum_buyamount.toFixed(2) + "<br />" + totalBuyAmount.toFixed(2), giveback: sum_giveback.toFixed(2) + "<br />" + totalGiveBack.toFixed(2), profit: sum_profit.toFixed(2) + "<br />" + totalProfit.toFixed(2) });
                if (a.navDatas && a.navDatas.length > 0) {
                    var html = "<a href='javascript:void(0)' onclick='showLowerList(" + rootuserid + ")'>本级</a>";
                    for (var i = 0; i < a.navDatas.length; i++) {
                        //最底层，不加链接
                        if (i == (a.navDatas.length - 1))
                            html += " > " + a.navDatas[i][1];
                        else
                            html += " > <a href='javascript:void(0)' onclick='showLowerList(" + a.navDatas[i][0] + ")'>" + a.navDatas[i][1] + "</a>";
                    }
                    jQuery("#tb_list").jqGrid("setCaption", html);
                }
                if (lowerid == rootuserid) {
                    //显示本级时，恢复导航栏
                    jQuery("#tb_list").jqGrid("setCaption", "<a href='javascript:void(0)' onclick='showLowerList(" + rootuserid + ")'>本级</a>");
                }
            }
        },
        onSelectRow: function(id) {
            if (id) {
                jQuery('#' + id).removeClass("ui-state-highlight");
                //alert(jQuery("#tb_list").jqGrid("getRowData", id).amount);
            }
        }
    });
    jQuery("#tb_list").jqGrid('navGrid', '#div_page', { edit: false, add: false, del: false }, {}, {}, {}, { closeOnEscape: true });

    //--盈亏明细
    jQuery("#tb_list2").jqGrid({
        url: getJQGridUrl2(),
        datatype: "json",
        colNames: ['游戏类型', '期号', '开奖号码','投注额','结果','我的返点', '我的奖金'],
        colModel: [
        { name: 'id', width: 140, align: "center", sortable: false, search: false },
        { name: 'dealid', width: 120, align: "center", sortable: false, searchoptions: { sopt: ["eq"]} },
    	{ name: 'profittime', width: 100, align: "center", sortable: false, search: false },
    	{ name: 'zy', width: 100, sortable: false, search: false },
    	{ name: 'zy', width: 80, sortable: false, search: false },
        { name: 'type', width: 100, align: "center", sortable: false, stype: "select", searchoptions: { sopt: ["eq"], value: { 1: "赢亏", 2: "返点", 3: "充值", 4: "提现"}} },
        { name: 'amount', width: 100, align: "right", sortable: false, search: false }],
        pager: "#div_page2",
        rowNum: 20,
        rowList: [20, 50, 100],
        height: 460,
        viewrecords: true,
        footerrow: true,
        sortname: "id",
        sortorder: "desc",
        caption: "<a href='javascript:void(0)' onclick='showLowerList(" + rootuserid + ")'>本级</a>",
        editurl: "#",
        loadJSONDataComplete: function(a) {             //服务端数据格式 did,profittime,zy,typeid,amount,amountlave
            if (a) {
                if (a.rows && a.rows.length > 0) {
                    var pos = (a.page - 1) * jQuery("#tb_list2").jqGrid()[0].p.rowNum + 1;
                    var profit = 0;
                    var index = 0;
                    for (var i = 0; i < a.rows.length; i++) {
                        var newcell = new Array();
                        index = 0;
                        newcell[index++] = pos++;
                        newcell[index++] = (a.rows[i].cell[3] == 1 || a.rows[i].cell[3] == 2) ? "<a href='#' onclick='showDealInfo(" + a.rows[i].cell[0] + ")'>" + a.rows[i].cell[0] + "</a>" : a.rows[i].cell[0];
                        newcell[index++] = a.rows[i].cell[1];
                        newcell[index++] = a.rows[i].cell[2];
                        newcell[index++] = getProfitTypeName(a.rows[i].cell[3]);
                        profit = parseFloat(a.rows[i].cell[4]);
                        newcell[index++] = profit < 0 ? "<span class='greenfont'>" + profit.toFixed(2) + "</span>" : profit.toFixed(2);
                        newcell[index++] = parseFloat(a.rows[i].cell[5]).toFixed(2);
                        a.rows[i].cell = newcell;
                    }
                }
                if (a.navDatas && a.navDatas.length > 0) {
                    var html = "<a href='javascript:void(0)' onclick='showLowerList(" + rootuserid + ")'>本级</a>";
                    for (var i = 0; i < a.navDatas.length; i++) {
                        //最底层，不加链接
                        html += " > <a href='javascript:void(0)' onclick='showLowerList(" + a.navDatas[i][0] + ")'>" + a.navDatas[i][1] + "</a>";
                    }
                    html += " > 盈亏明细";
                    jQuery("#tb_list2").jqGrid("setCaption", html);
                }
                if (lowerid == 0) {
                    //显示本级时，恢复导航栏
                    jQuery("#tb_list2").jqGrid("setCaption", "<a href='javascript:void(0)' onclick='showLowerList(" + rootuserid + ")'>本级</a>");
                }
            }
        },
        onSelectRow: function(id) {
            if (id) {
                jQuery('#' + id).removeClass("ui-state-highlight");
                //alert(jQuery("#tb_list").jqGrid("getRowData", id).amount);
            }
        }
    });
    jQuery("#tb_list2").jqGrid('navGrid', '#div_page2', { edit: false, add: false, del: false }, {}, {}, {}, { closeOnEscape: true });    
});
function getJQGridUrl(firstrequest) {
    return "/ajax.aspx?oper=jqgrid&jqgoper=getlowerprofit&lowerid=" + lowerid + "&firstrequest=" + firstrequest + "&dates=" + jQuery("#tb_dates").val() + "&datee=" + jQuery("#tb_datee").val() + "&nd=" + new Date().getTime();
}
function show_LowerList() {
    showLowerList(lowerid);
}
function showLowerList(id) {
    //显示用户列表
    if (jQuery("#tabs").tabs("option", "selected") != 0)
        jQuery("#tabs").tabs("select", 0);
    if (needrequest || lowerid != id) {
        //新用户，需要申请合计栏及导航栏
        lowerid = id;
        needrequest = false;
        jQuery("#tb_list").jqGrid("setGridParam", { url: getJQGridUrl(1), page: 1 }).trigger("reloadGrid");
    }
}
function addnewmember(data) {
    window.open("Member_Add.aspx?t=" + data + "&a=" + Math.random(), "frmMain");
}
function btSelectClick() {
    //选择新日期，重新查询   设置重装申请数据标志，以便切换时，自动刷新    
    needrequest = needrequest2 = true;
    if (jQuery("#tabs").tabs("option", "selected") == 0) {
        show_LowerList();
    }
    else {
        show_ProfitRecords();
    }
    if (btSelect)
        btSelect.attr("disabled", "disabled");
}

//---------盈亏明细相关函数
function show_ProfitRecords() {
    showProfitRecords(lowerid2);
}
function showProfitRecords(id) {
    if (jQuery("#tabs").tabs("option", "selected") != 1)
        jQuery("#tabs").tabs("select", 1);
    if (needrequest2 || lowerid2 != id) {
        lowerid2 = id;
        needrequest2 = false;
        $('#tabs').tabs("select", 1);
        jQuery("#tb_list2").jqGrid("setGridParam", { url: getJQGridUrl2(), page: 1 }).trigger("reloadGrid");
        //jQuery("#tb_list").jqGrid('navGrid', '#div_page', { edit: false, add: false, del: false });
    }
}
function getJQGridUrl2() {
    return "/ajax.aspx?oper=jqgrid&jqgoper=getlowerprofitrecord&lowerid=" + lowerid2 + "&dates=" + jQuery("#tb_dates").val() + "&datee=" + jQuery("#tb_datee").val() + "&nd=" + new Date().getTime();
}
